package com.junzhiit.nengji.cms.web.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.bainaeco.framework.util.JsonUtil;
import com.junzhiit.nengji.cms.service.IndustryService;
import com.junzhiit.nengji.cms.service.ShopService;
import com.junzhiit.nengji.cms.service.SystemUserService;
import com.junzhiit.nengji.cms.web.util.aliyun.SmsUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.GoodsListVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.ShopAddVO;
import com.junzhiit.nengji.cms.web.vo.ShopEditVO;
import com.junzhiit.nengji.cms.web.vo.ShopUpdateVO;
import com.junzhiit.nengji.cms.web.vo.ShopVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserAddVO;
import com.junzhiit.nengji.model.WechatConfig;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/shop")
public class ShopController extends BaseController {

	@Autowired
	private ShopService shopService;

	@Autowired
	private IndustryService industryService;
	
	@Autowired
	private SystemUserService systemUserService;

	@ApiOperation(value = "获取所有商户列表")
	@RequestMapping(method = RequestMethod.GET)
	public Map<String, Object> findAll(DataTablesParamVO dtp, ShopVO shopVO) {
		return shopService.findAll(dtp, shopVO);
	}
	
	@ApiOperation(value = "获取所有商户列表简洁信息不分页")
	@RequestMapping(value="/concise",method = RequestMethod.GET)
	public ResultVO findAllConcise() {
		return shopService.findAllConcise();
	}

	@ApiOperation(value="获得商户")
	@RequestMapping(value="/session",method=RequestMethod.GET)
	public ResultVO findById(String sessionId){
		return shopService.findById(sessionId);
	}

	@ApiOperation(value = "获取行业名称(下拉框)")
	@RequestMapping(value = "/get/industry/list", method = RequestMethod.GET)
	public ResultVO getIndustryFullName() {
		return industryService.getIndustryFullName(null);
	}

	@ApiOperation(value = "根据ID查询商户信息")
	@RequestMapping(value = "/info/id", method = RequestMethod.GET)
	public ResultVO getShopById(String id) {
		AssertUtil.notBlank(id, "id is required.");
		return shopService.getShopById(id);
	}

	@ApiOperation(value = "更新门店信息")
	@RequestMapping(method = RequestMethod.PUT)
	public ResultVO update(ShopUpdateVO shopUpdateVO) {
		ResultVO result = new ResultVO();
		result.setData(shopService.update(shopUpdateVO));
		return result;
	}

	@ApiOperation(value = "查询所有商户名称")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ResultVO getShopList() {
		return shopService.getShopList();
	}

	@ApiOperation(value = "查询店铺信息")
	@RequestMapping(value="/id",method = RequestMethod.GET)
	public ResultVO getById(String sessionId) {
		return shopService.getById(sessionId);
	}
	
	@ApiOperation(value = "编辑店铺信息")
	@RequestMapping(value = "/edit", method = RequestMethod.PUT)
	public ResultVO editShop(ShopEditVO shopEditVO) {
		return shopService.editShop(shopEditVO);
	}

	@ApiOperation(value = "删除店铺头像信息")
	@RequestMapping(value = "/head/img", method = RequestMethod.PUT)
	public ResultVO delShopHeadImgUrl(String shopHeadImgUrl, String sessionId) {
		return shopService.delShopHeadImgUrl(shopHeadImgUrl, sessionId);
	}
	
	@ApiOperation(value = "删除店铺背景图片")
	@RequestMapping(value = "/background/image", method = RequestMethod.PUT)
	public ResultVO delBackgroundImage(String shopHeadImgUrl, String sessionId) {
		return shopService.delBackgroundImage(shopHeadImgUrl, sessionId);
	}

	@ApiOperation(value = "删除店铺背景图片信息")
	@RequestMapping(value = "/banner/img", method = RequestMethod.PUT)
	public ResultVO delBannerImages(String bannerImage, String sessionId) {
		return shopService.delBannerImages(bannerImage, sessionId);
	}
	
	@ApiOperation(value = "店铺审核")
	@RequestMapping(value = "/merchants/audit", method = RequestMethod.POST)
	public ResultVO merchantsAudit(ShopVO shopVO) {
		return shopService.merchantsAudit(shopVO);
	}

	@ApiOperation(value = "查询店铺微信配置")
	@RequestMapping(value="/wechat/config",method = RequestMethod.GET)
	public ResultVO getWechatConfig(String sessionId) {
		return shopService.getShopWechatConfig(sessionId);
	}
	
	@ApiOperation(value = "新增或更新微信配置")
	@RequestMapping(value = "/add/or/update", method = RequestMethod.POST)
	public ResultVO addOrUpdateWechatConfig(WechatConfig wechatConfig,String sessionId) {
		return shopService.addOrUpdateWechatConfig(wechatConfig,sessionId);
	}
	
	@ApiOperation(value = "生成店铺二维码")
	@RequestMapping(value = "/create/qrcode", method = RequestMethod.POST)
	public ResultVO createQrcode(String shopId,String sessionId) {
		return shopService.createQrcode(shopId);
	}
	
	@ApiOperation(value = "查询所有待审核商品的商户名称(除去连锁品牌下的商户)")
	@RequestMapping(value = "/select", method = RequestMethod.GET)
	public ResultVO getGoodsAuditShopSelect() {
		return shopService.getGoodsAuditShopSelect();
	}
	
	@ApiOperation(value = "查询商户列表")
	@RequestMapping(value = "/get/list", method = RequestMethod.GET)
	public ResultVO getShopList(String sessionId,Integer type) {
		AssertUtil.notNull(sessionId, "sessionId is required.");
		return shopService.getShopList(sessionId,type);
	}
	
	@ApiOperation(value = "查询主体账号手机号码")
	@RequestMapping(value = "/get/phone", method = RequestMethod.GET)
	public ResultVO getShopPhone(String sessionId) {
		AssertUtil.notNull(sessionId, "shopId is required.");
		return shopService.getShopMainAccount(sessionId);
	}
	
	@ApiOperation(value="连锁商户-添加店铺")
	@RequestMapping(method=RequestMethod.POST)
	public ResultVO add(String systemUserAddVOStr,String shopAddVOStr,String setMealId,String sessionId){
		AssertUtil.notBlank("systemUserAddVOStr", "systemUserAddVOStr is required");
		AssertUtil.notBlank("shopAddVOStr", "shopAddVOStr is required");
		AssertUtil.notBlank("setMealId", "setMealId is required");
		SystemUserAddVO systemUserAddVO=JsonUtil.toObject(systemUserAddVOStr, SystemUserAddVO.class);
		ShopAddVO shopAddVO=JsonUtil.toObject(shopAddVOStr, ShopAddVO.class);
		return shopService.add(systemUserAddVO, shopAddVO, setMealId,sessionId);
	}
	
	
	@ApiOperation(value="连锁商户-发送添加验证码")
	@RequestMapping(value="/code/{mobilePhone}",method=RequestMethod.POST)
	public ResultVO sendAuthCode(@PathVariable String mobilePhone){
		ResultVO result=new ResultVO();
		systemUserService.checkPhone(mobilePhone);
		String smsModel=SmsUtil.ALIYUN_SMS_TEMPLATE_CODE;
		SmsUtil.sendPhoneCode(mobilePhone, smsModel);
		return result;
	}
	
	@ApiOperation(value="连锁商户 - 获取门店集合")
	@RequestMapping(value="/chain/list",method=RequestMethod.GET)
	public Map<String, Object> findShopList(DataTablesParamVO dtp,ShopVO shopVO){
		return shopService.findShopList(dtp,shopVO);
	}
	
	@ApiOperation(value="连锁商户-获取门店下的商品列表")
	@RequestMapping(value="/goods/list",method=RequestMethod.GET)
	public Map<String,Object> findGoodsListByShopId(DataTablesParamVO dtp,GoodsListVO glv){
		return shopService.findGoodsListByShopId(dtp,glv);
	}
	
	@ApiOperation(value="连锁商户-修改门店信息")
	@RequestMapping(value="/edit/info",method=RequestMethod.PUT)
	public ResultVO updateByShopId(ShopEditVO shopEditVO){
		return shopService.updateByShopId(shopEditVO);
	}
	
	@ApiOperation(value="获取小程序商家")
	@RequestMapping(value="program",method=RequestMethod.GET)
	public ResultVO getProgramShop(){
		return shopService.getProgramShop();
	}
	
	@ApiOperation(value="获取品牌下所有店铺列表(下拉框)")
	@RequestMapping(value="/get/shop/list",method=RequestMethod.GET)
	public ResultVO getShopList(String sessionId){
		return shopService.getShopList(sessionId);
	}
	
	@ApiOperation(value = "检查该登录对象为连锁平台总部还是连锁单店(0:连锁单店,1:连锁平台总部)")
	@RequestMapping(value = "/check", method = RequestMethod.GET)
	public ResultVO checkShopType(String sessionId) {
		return shopService.checkShopType(sessionId);
	}
	
	@ApiOperation(value="获取商户列表(模块店铺管理,区分平台)")
	@RequestMapping(value="/get/list/formodules",method=RequestMethod.GET)
	public Map<String,Object> getList(DataTablesParamVO dtp,ShopVO sv){
		return shopService.getList(dtp, sv);
	}
}
